package com.kefu.boss.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.boss.model.BossMenu;
import com.kefu.boss.model.BossRoleMenu;
import com.kefu.boss.service.BossMenuService;
import com.kefu.boss.service.BossRoleMenuService;
import com.kefu.boss.vo.BossMenuInsertVo;
import com.kefu.boss.vo.BossMenuUpdateVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@Api(tags = "业务支撑菜单")
@RestController
@RequestMapping("boss/menu")
public class BossMenuController extends AbstractBaseController {
    @Resource
    protected BossMenuService bossMenuService;
    @Resource
    protected BossRoleMenuService bossRoleMenuService;

    @PostMapping("/add")
    @ApiOperation(value = "新增功能菜单", notes = "新增功能菜单")
    public Result<String> menuAdd(@Validated @RequestBody BossMenuInsertVo bossMenuVo) {
        logger.info("新增功能菜单:{}", bossMenuVo);

        BossMenu info = bossMenuVo.convert();
        // 设置权重
        info.setWeight(bossMenuService.getMaxWeight(info.getParentId())+1);
        bossMenuService.save(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除功能菜单", notes = "页面判断如果有下级菜单，不让删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除功能菜单:{}", delete);

        bossMenuService.deleteById(delete.getId());
        // 删除角色菜单关联
        bossRoleMenuService.delete(Wrappers.lambdaQuery(BossRoleMenu.class).eq(BossRoleMenu::getMenuId,delete.getId()));
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改功能菜单", notes = "修改功能菜单")
    public Result<String> update(@Validated @RequestBody BossMenuUpdateVo bossMenuVo) {
        logger.info("修改功能菜单:{}", bossMenuVo);

        BossMenu info = bossMenuVo.convert();
        bossMenuService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "功能菜单详情", notes = "功能菜单详情")
    public Result<BossMenu> menuDetail(@Validated @RequestBody DetailVo vo) {

        return ResultGenerator.genSuccessResult(bossMenuService.getById(vo.getId()));
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询菜单", notes = "查询菜单")
    public Result<List<?>> list() {
        List<?> list = bossMenuService.listTree();
        return ResultGenerator.genSuccessResult(list);
    }
}
